package com.prothink.service.impl;

import com.prothink.dao.Userdao;
import com.prothink.entity.gradeEnum;
import com.prothink.entity.Menu;
import com.prothink.entity.TreeNode;
import com.prothink.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @description  ：用户相关实现
 * @author ：sumnight
 * @projectName ：UserService
 * @date ：2022/11/13 9:11
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    Userdao userdao;
    //获取菜单节点
    
    
    
    /**
     * @author: sumnight
     * @description: 根据权限获取对应菜单
     * @date: 2022/11/21 9:15
     * @params: [lv]
     * @return: java.util.List<com.prothink.entity.TreeNode>
     */
    @Override
    public List<TreeNode> getMenuTree(Integer lv) {
        List<Menu> all = new ArrayList<>();
        if (lv==5){
             all = userdao.getMenuByGrade(gradeEnum.GRADE_ADMIN.getGrade());
        }else if (lv==4){
            all = userdao.getMenuByGrade(gradeEnum.GRADE_HR.getGrade());
        }else if (lv==3){
            all = userdao.getMenuByGrade(gradeEnum.GRADE_OPERATE.getGrade());
        }else if (lv==2){
            all = userdao.getMenuByGrade(gradeEnum.GRADE_Teacher.getGrade());
        }
        return getAllNodes(all,0);
    }


    
    /**
     * @author: sumnight
     * @description: 修饰菜单层级
     * @date: 2022/11/21 9:16
     * @params:
     * @return: 
     */
    private List<TreeNode> getAllNodes(List<Menu> menus,int menuid){
        List<TreeNode> lists = new ArrayList<>();
        for (Menu menu:menus){
            if (menu.getMenuPId()==menuid){
                TreeNode node = new TreeNode();
                node.setId(menu.getId());
                node.setName(menu.getMenuName());
                node.setPath(menu.getMenuPath());
                node.setIcon(menu.getMenuIcon());
                node.setCode(menu.getMenuCode());
                node.setPid(menu.getMenuPId());
                List<TreeNode> childs = getAllNodes(menus,menu.getId());
                if (childs.size()==0){
                    node.setChildren(null);
                }else {
                    node.setChildren(childs);
                }
                lists.add(node);
            }
        }
        return lists;
    }
}
